# -*- coding: utf-8 -*-
# @时间：2023/11/7 9:30
# @作者：CJiaの可乐哥
# @文件名：login_log
# @软件：Pycharm2023
from typing import List

from django.shortcuts import get_object_or_404
from ninja import Router, Schema, Query, Field, ModelSchema
from ninja.pagination import paginate
from system.models import LoginLog

from utils.cj_curd import retrieve
from utils.cj_ninja import MyPagination
from utils.cj_response import SuccessResponse

router = Router()


class Filters(Schema):
    id: str = Field(None, alias="login_log_id")
    name: str = Field(None, alias="name")
    code: str = Field(None, alias="code")


class SchemaOut(ModelSchema):
    class Config:
        model = LoginLog
        model_fields = "__all__"


@router.get("/login_log/{login_log_id}", response=SchemaOut)
def get_login_log(request, login_log_id: int):
    qs = get_object_or_404(LoginLog, id=login_log_id)
    return qs


@router.get("/login_log", response=List[SchemaOut])
@paginate(MyPagination)
def list_login_log(request, filters: Filters = Query(...)):
    qs = retrieve(request, LoginLog, filters)
    return qs
